package com.base.controller;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.helper.BaiduMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.base.comm.CommContent;
import com.base.po.voip.SelfShop;
import com.base.service.LogsService;
import com.base.service.SelfShopService;
import com.base.utils.IP;
import com.base.utils.Produce;

@Controller
@RequestMapping("/admin")
public class update_SelfShop {
    @Autowired
    private LogsService logsService;// log日志
    @Autowired
    private SelfShopService selfShopService;

    String SUCCESS = "success";

    // 更改商户信息
    @RequestMapping(value = "/updateSelfShop", method = RequestMethod.POST)
    public void updateSelfShop(HttpServletRequest request, HttpServletResponse response,
                               @RequestParam(value = "id", required = true) String id, String shopname, String shoplogo,
                               String shopbg, String address, String contacts, String phonenum, Byte isopen,
                               Double longitude, Double latitude, String busintime, String discount,
                               Byte datastatus, String introduction, Byte ischoice, Boolean sort, Integer falg, String catid,
                               String attestation1, String attestation2, String attestation3, String attestation4) throws Exception {
        // 返回参数
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
//		String adminUserId = (String) request.getSession().getAttribute("adminUserId");

        SelfShop selfShop = selfShopService.selectByPrimaryKey(id);
        PrintWriter out = response.getWriter();

        Map<String, Object> jsonMap = null;
        String city = null;
        String district = null;
        try {
            jsonMap = BaiduMapHelper.getCity(request, longitude, latitude);
            city = (String)jsonMap.get("city");
            district = (String)jsonMap.get("district");
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (attestation1 == "" || attestation1 == "undefined") {
            attestation1 = null;
        }
        if (attestation2 == "" || attestation2 == "undefined") {
            attestation2 = null;
        }
        if (attestation3 == "" || attestation3 == "undefined") {
            attestation3 = null;
        }
        if (attestation4 == "" || attestation4 == "undefined") {
            attestation4 = null;
        }

        if (id == null) {
            out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
            return;
        } else {
            selfShop.setShopname(shopname);
            selfShop.setShoplogo(shoplogo);
            selfShop.setShopbg(shopbg);
            selfShop.setAddress(address);
            selfShop.setCity(city);
            selfShop.setDistrict(district);
            selfShop.setLongitude(longitude);
            selfShop.setLatitude(latitude);
            selfShop.setContacts(contacts);
            selfShop.setPhonenum(phonenum);
            selfShop.setIntroduction(introduction);
            selfShop.setBusintime(busintime);
            selfShop.setDiscount(discount);
            selfShop.setIschoice(ischoice);
            selfShop.setAttestation1(attestation1);
            selfShop.setAttestation2(attestation2);
            selfShop.setAttestation3(attestation3);
            selfShop.setAttestation4(attestation4);
            if (catid != null) {
                selfShop.setCatid(catid);
            }
            if (sort != null) {
                selfShop.setSort(sort);
            }
            if (isopen != null) {
                selfShop.setIsopen(isopen);
                selfShop.setRespondtime(Produce.getNowDate());
            }
            selfShop.setDatastatus(datastatus);
            if (sort != null && sort == true) {
                if (falg == null) {
                    falg = 0;
                } else {
                    falg = 1;
                }
                if (selfShopService.countSort((String) request.getSession().getAttribute("adminUserId")) + falg >= 11) {
                    out.print("success2");
                    return;
                }
            }
            int code = selfShopService.updateByPrimaryKeySelective(selfShop);
            String usernamesys = (String) request.getSession().getAttribute("useracc");
            if (code == 1) {
                logsService.insert(CommContent.TYPE_UPDATE, "修改" + shopname + "店铺信息成功", usernamesys, true,
                        IP.getIpAddr(request));
                out.print(SUCCESS);
            } else {
                logsService.insert(CommContent.TYPE_UPDATE, "修改" + shopname + "店铺信息失败", usernamesys, true,
                        IP.getIpAddr(request));
                out.print("更改失败，请您重试");
            }
        }
    }
}
